package com.qqtop10.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.AdminRole;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.RebateLink;
import com.qqtop10.manager.model.temp.Goods;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.AdminService;
import com.qqtop10.manager.service.GoodsService;
import com.qqtop10.manager.service.RebateLinkService;

/**
 * 
 * @ClassName: AdminController
 * @Description: AdminController
 * 
 */
@RequestMapping("/manager/admin/")
@Controller
public class AdminController extends BaseController {
	@Autowired
	private AdminService adminService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(Admin input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			int count = adminService.count(input);
			List<Admin> array = adminService.all(input, pagingArgs);
			for (Admin item : array) {
				AdminRole adminRole=new AdminRole();
				adminRole.setAdmin_uuid(item.getUuid());
				List<AdminRole> roles= adminService.getAdminRole(adminRole);
				String roleStr = "";
				for (AdminRole role : roles) {
					roleStr += role.getRole_uuid() + ",";
				}
				item.setRoles(roleStr);
			}
			return page(count, array);
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("add.do")
	@ResponseBody
	public Result AddAdmin(HttpServletRequest request, HttpServletResponse response,Admin input) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/javascript; charset=utf-8");
		int res = 0;
		String account = (input.getAccount());
		input.setAccount(account);
		String password = (input.getPassword());
		password = md5(password).toLowerCase();
		input.setPassword(password);
		input.setUuid(newId());
		input.setCreatetime(now());
		input.setCreator(getLogindUuid());
		
		
		Admin t=adminService.get(input);
		if(t!=null){
			return err("账号已存在！");
		}
		
		String roles = this.getRequest().getParameter("roles");
		if (roles == null) {
			return err("添加失败");
		}
		String[] rolelist = roles.split(",");
		for (String item : rolelist) {
			AdminRole adminRole = new AdminRole();
			adminRole.setAdmin_uuid(input.getUuid());
			adminRole.setRole_uuid(item);
			adminRole.setUuid(newId());
			adminRole.setCreatetime(now());
			adminRole.setCreator(getLogindUuid());
			res = adminService.insertAdminRole(adminRole);
		}
		adminService.deleteAdminByuuid(input);
		res = adminService.insert(input);
		if (res > 0) {
			return ok("操作成功");
		} else {
			return err("添加失败");
		}
	}

	@RequestMapping("update.do")
	@ResponseBody
	public Result UpdateAdmin(Admin input) {
		int res = 0;
		String roles = this.getRequest().getParameter("roles");
		if (roles == null) {
			return err("修改失败");
		}
		String[] rolelist = roles.split(",");
		for (String item : rolelist) {
			AdminRole adminRole = new AdminRole();
			adminRole.setAdmin_uuid(input.getUuid());
			adminRole.setRole_uuid(item);
			adminRole.setUuid(newId());
			adminRole.setCreatetime(now());
			adminRole.setCreator(getLogindUuid());
			res = adminService.insertAdminRole(adminRole);
		}
		if (res > 0) {
			return ok("操作成功");
		} else {
			return err("修改失败");
		}
	}
	
	@RequestMapping("delete.do")
	@ResponseBody
	public Result DeleteById(int id) {
		int ret = adminService.delete(id);
		if (ret > 0) {
			return ok("删除成功");
		} else {
			return err("操作失败");
		}
	}
}
